export class TimeTool {

  /**
   * 当前时间戳
   *
   * @returns 毫秒级时间戳
   */
  now(): number {
    return new Date().getTime()
  }

  /**
   * 当前时间格式化(yyyy-MM-dd HH:mm:ss)
   *
   * @return
   */
  formatNowTime() : string {
    return this.format(this.now())
  }

  /**
   * 时间戳转成时间
   *
   * @param ms - 毫秒级时间戳
   * @returns 格式化后的时间字符串
   */
  format(ms: number): string {
    const d = new Date(ms)
    const year = d.getFullYear()
    const month = String(d.getMonth() + 1).padStart(2, '0')
    const date = String(d.getDate()).padStart(2, '0')
    const hour = String(d.getHours()).padStart(2, '0')
    const minute = String(d.getMinutes()).padStart(2, '0')
    const second = String(d.getSeconds()).padStart(2, '0')

    return `${year}-${month}-${date} ${hour}:${minute}:${second}`
  }

  /**
   * Date to Week
   *
   * @param date - 当前日期对象
   * @returns {string} 星期几
   */
  toWeek(date: Date): string {
    if (date instanceof Date) {
      const dayNames: string[] = [
        '星期天',
        '星期一',
        '星期二',
        '星期三',
        '星期四',
        '星期五',
        '星期六',
      ]
      return dayNames[date.getDay()]
    } else {
      throw new TypeError('Param error, expected a Date object!')
    }
  }
}
